<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\Pay;
use app\model\PuddleOrders;
use app\model\User;
use app\model\Vip;
use app\model\VipOption;
use app\model\VipOrders;
use app\model\VipOrdersDetail;
use app\tool\Random;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use think\db\Query;
use think\facade\Db;
use think\facade\Log;

class VipController extends BaseController
{
	function index()
	{
		$rows = Vip::with(['detail' => function (Query $query) {
			$query->order(['weigh' => 'desc']);
		}, 'option' => function (Query $query) {
			$query->order(['weigh' => 'desc']);
		}])->order(['weigh' => 'desc'])->select();
		$this->writeJson($rows);
	}
	
	function createOrder()
	{
		$option_id = $this->request->post('option_id/d');
		$pay_type = $this->request->post('pay_type/d'); //支付方式
		$vipoption = VipOption::findOrFail($option_id);
		$user = User::findOrFail($this->user_id);
		if ($user->level > $vipoption->vip->id) {
			$this->writeJson(0,'不能降级充值');
		}
		$ordersn = Random::ordersn();
		$pay_amount = $vipoption->price;
		$data = [
			'user_id' => $this->user_id,
			'option_id' => $vipoption->id,
			'pay_amount' => $pay_amount,
			'order_sn' => $ordersn,
		];
		// 启动事务
		Db::startTrans();
		try {
			$order = VipOrders::create($data);
			$ret = Pay::pay($pay_type, $pay_amount, $ordersn, '购买会员', 'vip', $user->openid);
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			$this->writeJson(0, $e->getMessage());
		}
		$this->writeJson($ret);
	}
	
	#选择钓位
	function choose()
	{
		$puddle_detail_id = $this->request->post('puddle_detail_id/d');
		$user = User::findOrFail($this->user_id);
		if ($user->level != 2) {
			$this->writeJson(0, '您不是Vip会员');
		}
		if ($user->puddle_detail_id != 0) {
			$this->writeJson(0, '您已经选择过场地');
		}
		$user->puddle_detail_id = $puddle_detail_id;
		$user->save();
		$this->writeJson();
	}
	
	#获取核销码
	function getQrCode()
	{
		$user = User::findOrFail($this->user_id);
		if ($user->level != 2) {
			$this->writeJson(0, '您不是Vip会员');
		}
		$writer = new PngWriter();
		$qrCode = QrCode::create(json_encode(['action' => 'vip_order_off', 'user_id' => $user->id]))//跳转的url地址
		->setSize(100)      //大小
		->setMargin(0)             //边距
		->setForegroundColor(new Color(0, 0, 0))        //前景色
		->setBackgroundColor(new Color(255, 255, 255));       //背景色
		$result = $writer->write($qrCode)->getDataUri();
		$this->writeJson(['qrdata' => $result, 'puddle' => $user->puddle_detail->puddle->name, 'detail' => $user->puddle_detail->name]);
	}
	
	
	#核销
	function qrCodeOff()
	{
		$content = $this->request->post('content');
		if (empty($content)) {
			$this->writeJson(0, '扫码内容为空');
		}
		$user = User::find($this->user_id);
		if (!$user) {
			$this->writeJson(401, '重新登陆');
		}
		if ($user->type != 2) {
			$this->writeJson(0, '你没有核销权限');
		}
		
		$content = json_decode($content);
		if (!isset($content->action)) {
			$this->writeJson(0, '参数错误');
		}
		switch ($content->action) {
			case 'puddle_order_off':
				$order = PuddleOrders::where(['order_sn' => $content->order_sn])->find();
				if (!$order) {
					$this->writeJson(0, '未找到此订单');
				}
				if ($order->pay_status != 2) {
					$this->writeJson(0, '此订单已被核销');
				}
				$order->pay_status = 3;
				$order->qrofftime = time();
				$order->save();
				break;
			case 'vip_order_off':
				$user = User::find($content->user_id);
				if (!$user) {
					$this->writeJson(0, '未找到此用户');
				}
				if ($user->level != 3) {
					$this->writeJson(0, '此用户不是Vip会员');
				}
				VipOrdersDetail::create([
					'user_id' => $user->id,
				]);
				break;
			default:
				$this->writeJson(0, '参数错误');
		}
		
		$this->writeJson(1, '核销成功');
		
	}
	
	#核销码信息
	function qrCodeDetail()
	{
		$content = $this->request->get('content');
		if (empty($content)) {
			$this->writeJson(0, '扫码内容为空');
		}
		$user = User::find($this->user_id);
		if (!$user) {
			$this->writeJson(401, '重新登陆');
		}
		if ($user->type != 2) {
			$this->writeJson(0, '你没有核销权限');
		}
		
		$content = json_decode($content);
		if (!isset($content->action)) {
			$this->writeJson(0, '参数错误');
		}
		switch ($content->action) {
			case 'puddle_order_off':
				$order = PuddleOrders::where(['order_sn' => $content->order_sn])->find();
				if (!$order) {
					$this->writeJson(0, '未找到此订单');
				}
				$data = ['puddle' => $order->detail->puddle->name, 'detail' => $order->detail->name, 'createtime' => $order->createtime, 'date' => $order->day];
				break;
			case 'vip_order_off':
				$user = User::find($content->user_id);
				if (!$user) {
					$this->writeJson(0, '未找到此用户');
				}
				$data = ['puddle' => $user->puddle_detail->puddle->name, 'detail' => $user->puddle_detail->name, 'createtime' => date('Y-m-d H:i:s'), 'date' => date('Y-m-d')];
				break;
			default:
				$this->writeJson(0, '参数错误');
		}
		$this->writeJson($data);
	}
	
	#核销记录
	function detail()
	{
		$rows = VipOrdersDetail::where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select();
		$this->writeJson($rows);
	}
	
}
